package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.common.ApplicationContext;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.pojo.Orders;
import com.ithiema.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize){
        Long userId = (Long) ApplicationContext.getData();
        log.info("查询当前用户订单信息,userId:{}",userId);
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,wrapper);

        return R.success(ordersPage);
    }

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }
}
